package in.ac.dei.edrp.cms.controller.activitymaster;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import in.ac.dei.edrp.cms.constants.CRConstant;
import in.ac.dei.edrp.cms.dao.activitymaster.StartActivityDao;
import in.ac.dei.edrp.cms.domain.activitymaster.ActivityMaster;
import in.ac.dei.edrp.cms.domain.activitymaster.CountProcessRecorList;
import in.ac.dei.edrp.cms.domain.activitymaster.StartActivityBean;
import in.ac.dei.edrp.cms.utility.RegistrationFunction;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.ibatis.sqlmap.client.SqlMapClient;

public class StartActivityController extends MultiActionController{

	public static final Logger logger = Logger.getLogger(StartActivityController.class);
	
	private StartActivityDao startActivityDao;

	public void setStartActivityDao(StartActivityDao startActivityDao) {
		this.startActivityDao = startActivityDao;
	}
	
	public ModelAndView getProcesses(HttpServletRequest request,
			HttpServletResponse response)throws Exception {
		HttpSession session=request.getSession(true);
		
		String universityId=(String)session.getAttribute("universityId");
		String userId=(String)session.getAttribute("userId");
		//System.out.println("Coming in process");
		StartActivityBean startActivityBean=new StartActivityBean();
		startActivityBean.setUniversityId(universityId);
		startActivityBean.setGroupCode(CRConstant.PROCESS_MASTER);
		//Since code for process master is attached, so process id will be in list
		List<StartActivityBean> processList = startActivityDao.getProcessList(startActivityBean);
//		for(StartActivityBean sta:processList){
//		System.out.println("Coming here"+sta.getComponentDescription());
//		}
		return new ModelAndView("activitymaster/ComponentList", "itemList",processList);
	
	}
	
	public ModelAndView getSemesterList(HttpServletRequest request,
			HttpServletResponse response)throws Exception {
		HttpSession session=request.getSession(true);
		
		String universityId=(String)session.getAttribute("universityId");
		String userId=(String)session.getAttribute("userId");
		
		StartActivityBean startActivityBean=new StartActivityBean();
		startActivityBean.setUniversityId(universityId);
		startActivityBean.setGroupCode(CRConstant.SEMESTER_GROUP_CODE);
		
		//Since code for process master is attached, so semester codes will be in list
		List<StartActivityBean> semesterList = startActivityDao.getProcessList(startActivityBean);
		
		return new ModelAndView("activitymaster/ComponentList", "itemList",semesterList);
	
	}
	
	/**
     * It returns data for process grid list of start activity screen
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
	public ModelAndView processList(HttpServletRequest request,
			HttpServletResponse response)throws Exception {
		RegistrationFunction registrationFunction=new RegistrationFunction();
		
		HttpSession session=request.getSession(true);
		
		String universityId=(String)session.getAttribute("universityId");
		String userId=(String)session.getAttribute("userId");
				
		
		String semesterStartDate="";
		String semesterEndDate="";
		
		//programProcessList holds processGridList values for client side
		List<StartActivityBean> programProcessList=new ArrayList<StartActivityBean>();
		//startActivityKey will be used to get program course key
		//if branch is null, then keys for all branches and specialization for selected program will be in list
		//if only specialization is null, then keys for all specialization for selected program and branches will be in list
		StartActivityBean startActivityKey=new StartActivityBean((String)request.getParameter("programId"),
		(String)request.getParameter("branchId"),(String)request.getParameter("specializationId"),
		(String)request.getParameter("semesterId"));
		
		//setting university id
		startActivityKey.setUniversityId(universityId);
		
		
		//getting session start date and end date from university_master table for current_status=1
		//setting values of session start date and session end date
		System.out.println((String)request.getParameter("specializationId")+(String)request.getParameter("programId")+(String)request.getParameter("branchId")+(String)request.getParameter("semesterId")+(String)request.getParameter("processId"));
		List<StartActivityBean> programCourseKey=startActivityDao.getProgramCourseKey(startActivityKey);
		for(StartActivityBean programCourseList:programCourseKey){
			
			//Setting program course key in startActivityKey object
			startActivityKey.setProgramCourseKey(programCourseList.getProgramCourseKey());
			
			//setting session start and end date for program course key
			List<StartActivityBean> sessionList=startActivityDao.getSessionDate(universityId);
			for(StartActivityBean sessionListBean:sessionList){
				startActivityKey.setSessionStartDate(sessionListBean.getSessionStartDate());
				startActivityKey.setSessionEndDate(sessionListBean.getSessionEndDate());
				System.out.println(sessionListBean.getSessionStartDate()+sessionListBean.getSessionEndDate());
			}
			
			//Getting semester start and end date for program course key and semester start date (using startActivityKey)
			List<StartActivityBean> semesterList=startActivityDao.getSemesterDate(startActivityKey);
			for(StartActivityBean semesterListBean:semesterList){
				semesterStartDate=semesterListBean.getSemesterStartDate();
				semesterEndDate=semesterListBean.getSemesterEndDate();
				System.out.println(semesterStartDate+semesterEndDate);
				
			}
			
			//Object prepared to get list of program, branch and specialization for given input
			StartActivityBean startActivityBean=new StartActivityBean(universityId,(String)request.getParameter("entityId"),
				programCourseList.getProgramCourseKey(),
				semesterStartDate,semesterEndDate,
				(String)request.getParameter("processId"));
			
			
		
		
		System.out.println(programCourseList.getProgramCourseKey());
		//Here list of branch and specialization for given program is now set in startActivityBean2 object	
		List<StartActivityBean> programCourseHeaderList = startActivityDao.getProcessGridList(startActivityBean);
		 for(StartActivityBean processGridList:programCourseHeaderList){
			 //setting semesterStartDate and semesterEndDate and programCourseKey in processGridList
			 processGridList.setSemesterStartDate(semesterStartDate);
			 processGridList.setSemesterEndDate(semesterEndDate);
			 processGridList.setProgramCourseKey(programCourseList.getProgramCourseKey());
			 //processGridList object is added into programProcessList
			 programProcessList.add(processGridList);
		 }
		
		}
		
		//System.out.println(programProcessList);
		return new ModelAndView("activitymaster/ProcessGrid","prcessGridList", programProcessList);
	}
	
	/**
     * It returns data for process grid list of start activity screen
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
	public ModelAndView activityList(HttpServletRequest request,
			HttpServletResponse response)throws Exception {
		System.out.println("Coming in activity list");
		RegistrationFunction registrationFunction=new RegistrationFunction();
		HttpSession session=request.getSession(true);
		
		String universityId=(String)session.getAttribute("universityId");
		String userId=(String)session.getAttribute("userId");
					
		StartActivityBean startActivityBean=new StartActivityBean(universityId,(String)request.getParameter("entityId"),
				(String)request.getParameter("programCourseKey"),
				(String)request.getParameter("semesterStartDate"),(String)request.getParameter("semesterEndDate"),
				(String)request.getParameter("processId"));
		
		
		List<StartActivityBean> sessionList=startActivityDao.getSessionDate(universityId);
		for(StartActivityBean startBean:sessionList){
			startActivityBean.setSessionStartDate(startBean.getSessionStartDate());
			startActivityBean.setSessionEndDate(startBean.getSessionEndDate());
		}
		
		//System.out.println((String)request.getParameter("entityId")+(String)request.getParameter("programCourseKey")+(String)request.getParameter("semesterStartDate")+(String)request.getParameter("semesterEndDate")+(String)request.getParameter("processId"));
		//List contains activities for given input and sent to clinet side
		List<StartActivityBean> processActivityList = startActivityDao.getProcessActivityGridList(startActivityBean);
		
		for(StartActivityBean star: processActivityList){
			System.out.println(star.getActivityId());
		}
		return new ModelAndView("activitymaster/ProcessActivityList","processActivityList", processActivityList);
	
	}
	
	/**
	 * This method sets required input values according to user request and calls startActivityMethod
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView startProcessActivity(HttpServletRequest request,
			HttpServletResponse response)throws Exception {
		
		HttpSession session=request.getSession(true);
		
		String universityId=(String)session.getAttribute("universityId");
		String userId=(String)session.getAttribute("userId");
		CountProcessRecorList countList=new CountProcessRecorList();
		
	
//		String programId="";
//		String branchId="";
//		String specializationId="";
//		String semesterCode="";
					
		try{
			
			//startActivityBean contains following properties:
			//1-universityId, 2-entityId, 3- programId, 4-branchId, 5-specializationId, 6-SemesterCode
			//7-programCourseKey
			//8-sessionStartDate,9-sessionEndDate
			//10-semesterStartDate,11-semesterEndDate
			//12-processId
			//13-activityId,14-activitySequence,15-activityStatus
			//16-programName, 17-branchName, 18-specializationName, 19-semesterName, 18- entityName
			System.out.println(universityId+
					request.getParameter("entityId")+request.getParameter("entityName")+
					request.getParameter("programId")+request.getParameter("programName")+
					request.getParameter("branchId")+request.getParameter("branchName")+
					request.getParameter("specializationId")+request.getParameter("specializationName")+
					request.getParameter("semesterId")+request.getParameter("semesterName")+
					request.getParameter("semesterStartDate")+request.getParameter("semesterEndDate")+
					request.getParameter("programCourseKey")+
					request.getParameter("processId")+request.getParameter("activityCode")+
					Integer.parseInt((String)request.getParameter("activitySequence"))+request.getParameter("activityStatus"));
			
			StartActivityBean startActivityBean=new StartActivityBean(universityId,
					(String)request.getParameter("entityId"),(String)request.getParameter("entityName"),
					(String)request.getParameter("programId"),(String)request.getParameter("programName"),
					(String)request.getParameter("branchId"),(String)request.getParameter("branchName"),
					(String)request.getParameter("specializationId"),(String)request.getParameter("specializationName"),
					(String)request.getParameter("semesterId"),(String)request.getParameter("semesterName"),
					(String)request.getParameter("semesterStartDate"),(String)request.getParameter("semesterEndDate"),
					(String)request.getParameter("programCourseKey"),
					(String)request.getParameter("processId"),(String)request.getParameter("activityCode"),
					Integer.parseInt((String)request.getParameter("activitySequence")),(String)request.getParameter("activityStatus"),userId);
			
			//getting session start date and end date from university_master table for current_status=1
			//setting values of session start date and session end date
			List<StartActivityBean> sessionList=startActivityDao.getSessionDate(universityId);
			for(StartActivityBean startBean:sessionList){
				startActivityBean.setSessionStartDate(startBean.getSessionStartDate());
				startActivityBean.setSessionEndDate(startBean.getSessionEndDate());
			}
			
			//setting values for program id, branch id,specialization id, semester code for program course key
//			List<StartActivityBean> programvalueList=registrationFunction.getProgramForKey(startActivityBean);
//			for(StartActivityBean idList:programvalueList){
//				startActivityBean.setProgramId(idList.getProgramId());
//				startActivityBean.setBranchId(idList.getBranchId());
//				startActivityBean.setSpecializationId(idList.getSpecializationId());
//				startActivityBean.setSemesterCode(idList.getSemesterCode());
//			}
		
			//startActivityFunction triggers from this point. and before this all values are set in startActivityBean object
	// added by pachauri without implementing count
	if(startActivityBean.getProcessId().equalsIgnoreCase("REG") && startActivityBean.getActivityId().equalsIgnoreCase("PDF")){
				System.out.println("inside if");
				return new ModelAndView("studentCoursePdf","temporaryInfo",startActivityBean);
			}
	//end 
		countList=startActivityDao.startActivity(startActivityBean);
		
		}catch(Exception e){
			System.out.println("Exception is "+e.getMessage());
		}
		
		return new ModelAndView("activitymaster/CountProcessedRecord","countList", countList);
	}
	
}
